package com.paipai.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
 

/**
 * 订单编码码生成器，生成32位数字编码，
 * @生成规则 1位单号类型+17位时间戳+14位(用户id加密&随机数)
 * Date:2017年9月8日上午10:05:19
 * @author jiwengjian
**/



public class OrderCodeFactory {
	
	
	
	/** 璁㈠崟绫诲埆澶� */
	    private static final String ORDER_CODE = "1";
	    /** 閫�璐х被鍒ご */
	    private static final String RETURN_ORDER = "2";
	    /** 閫�娆剧被鍒ご */
	    private static final String REFUND_ORDER = "3";
	    /** 鏈粯娆鹃噸鏂版敮浠樺埆澶� */
	    private static final String AGAIN_ORDER = "4";
	    /** 闅忓嵆缂栫爜 */
	    private static final int[] r = new int[]{7, 9, 6, 2, 8 , 1, 3, 0, 5, 4};
	    /** 鐢ㄦ埛id鍜岄殢鏈烘暟鎬婚暱搴� */
	    private static final int maxLength = 14;
	    
	    /**
	     * 更具id进行加密+加随机数组成固定长度编码
	     */
	    private static String toCode(Long id) {
	        String idStr = id.toString();
	        StringBuilder idsbs = new StringBuilder();
	        for (int i = idStr.length() - 1 ; i >= 0; i--) {
	            idsbs.append(r[idStr.charAt(i)-'0']);
	        }
	        return idsbs.append(getRandom(maxLength - idStr.length())).toString();
	    }
	     
	    /**
	     * 生成时间戳
	     * @param n 长度
	     */
	    private static String getDateTime(){
	        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	        return sdf.format(new Date());
	    }
	    
	    /**
	     * 生成固定长度随机码
	     * @param n    长度
	     */
	    public static Long getRandom(long n) {
	        long min = 1,max = 9;
	        for (int i = 1; i < n; i++) {
	            min *= 10;
	            max *= 10;
	        }
	        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min ;
	        String order = rangeLong+getDateTime();
	        return Long.parseLong(order);
	    }

	
	public static void main(String[] args) {
		System.out.println(getRandom(5L));
	}
	
	

}
